package org.jeecg.modules.middle.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.implementation.bytecode.Throw;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.entity.CaishuiBusinessUser;
import org.jeecg.modules.middle.business.mapper.CaishuiBusinessUserMapper;
import org.jeecg.modules.middle.business.service.ICaishuiBusinessUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 商户用户表
 * @Author: yfkj
 * @Date:   2023-02-07
 * @Version: V1.0
 */
@Service
public class CaishuiBusinessUserServiceImpl extends ServiceImpl<CaishuiBusinessUserMapper, CaishuiBusinessUser> implements ICaishuiBusinessUserService {

	@Resource
	private CaishuiBusinessUserMapper caishuiBusinessUserMapper;
	//注入feign客户端
	@Resource
	private ISysBaseAPI iSysBaseAPI;


	/**
	 * 外键关联分页查询
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public IPage<CaishuiBusinessUser> pageMultiTable(IPage<CaishuiBusinessUser> page, QueryWrapper<CaishuiBusinessUser> queryWrapper) {
		return this.baseMapper.pageMultiTable(page, queryWrapper);
	}

	@Override
	public List<CaishuiBusinessUser> selectByMainId(String mainId) {
		return caishuiBusinessUserMapper.selectByMainId(mainId);
	}

	@Override
	@Transactional
	public Result<?> addBusinessUser(CaishuiBusinessUser caishuiBusinessUser) {
		LoginUser loginUser = new LoginUser();
		loginUser.setUsername(caishuiBusinessUser.getPhone());
		loginUser.setRealname(caishuiBusinessUser.getRealname());
		loginUser.setPassword(caishuiBusinessUser.getPassword());
		loginUser.setPhone(caishuiBusinessUser.getPhone());
//		loginUser.setNickname(TokenUtils.getLoginUser().getNickname());
//		loginUser.setAvatar(TokenUtils.getLoginUser().getAvatar());
		loginUser.setRelTenantIds(TokenUtils.getLoginUser().getRelTenantIds());
		loginUser.setUserType(3);
		Result result = iSysBaseAPI.addBUser(loginUser);//Feign 调用远程微服务接口
		if(oConvertUtils.isEmpty(result)){
			return Result.error("添加失败！");
		}else if(!result.isSuccess()){
			return Result.error(result.getMessage());
		}else  {
			String userId = result.getResult().toString();//用户ID
			if("10001".equals(result.getMessage())){//登录用户存在的情况下再判断商户用户是否存在
				long count = caishuiBusinessUserMapper.selectCount(QueryGenerator.initQueryWrapper(caishuiBusinessUser,null).lambda()
						.eq(CaishuiBusinessUser::getBusinessId,caishuiBusinessUser.getBusinessId())
						.eq(CaishuiBusinessUser::getUserId,userId));
				if(count > 0){
					return Result.error("用户已存在！");
				}
			}
			caishuiBusinessUser.setUserId(userId);
			caishuiBusinessUserMapper.insert(caishuiBusinessUser);
			return Result.ok("添加成功！");
		}
	}

	@Override
	@Transactional
	public Result<?> editBusinessUser(CaishuiBusinessUser caishuiBusinessUser) {
		LoginUser loginUser = new LoginUser();
		loginUser.setUsername(caishuiBusinessUser.getPhone());
		loginUser.setRealname(caishuiBusinessUser.getRealname());
		loginUser.setPhone(caishuiBusinessUser.getPhone());
		loginUser.setId(caishuiBusinessUser.getUserId());
		loginUser.setUserType(3);
		Result result = iSysBaseAPI.editBUser(loginUser);//Feign 调用远程微服务接口
		if(oConvertUtils.isEmpty(result)){
			return Result.error("编辑失败！");
		}else if(!result.isSuccess()){
			return Result.error(result.getMessage());
		}else  {
			caishuiBusinessUserMapper.updateById(caishuiBusinessUser);
			return Result.ok("编辑成功！");
		}
	}

	@Override
	@Transactional
	public Result<?> deleteBusinessUser(String id) {
		CaishuiBusinessUser businessUser = caishuiBusinessUserMapper.selectById(id);
		if(oConvertUtils.isEmpty(businessUser)){
			return Result.error("移除失败！");
		}
		String userId = businessUser.getUserId();//用户ID
		caishuiBusinessUserMapper.deleteById(businessUser);
		//如果该用户对于的商户用户不存在了，则删除登录用户
		long count = caishuiBusinessUserMapper.selectCount(QueryGenerator.initQueryWrapper(businessUser,null).lambda()
				.eq(CaishuiBusinessUser::getUserId,userId));
		if(count < 1){
			Result result = iSysBaseAPI.deleteBUser(userId);//Feign 调用远程微服务接口
			if(oConvertUtils.isEmpty(result)){
				return Result.error("移除失败！");
			}else if(!result.isSuccess()){
				throw new JeecgBootException(result.getMessage());
				//return Result.error(result.getMessage());
			}
		}
		return Result.ok("移除成功！");
	}
}
